package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.view.ui.adapter.pick.PickAreaAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;
import com.swgk.core.util.PickSelectEntity;

import java.util.List;

public class PickAreaPopWindow extends BasePopWindow {

    private RecyclerView recyclerView;
    private RecyclerView cityList;
    private LinearLayout ll_next;
    private TextView tv_next;
    private TextView tv_ok;
    private LinearLayout bottomLayout;
    private PickAreaAdapter pickProvinceAdapter;
    private PickAreaAdapter pickCityAdapter;
    private List<PickSelectEntity> cityDatas;
    private String oldNo = "";
    private String no = "";
    private String oneNo = "";
    private String towNo = "";
    private String name = "";
    private int position = -1;
    private boolean isNext;
    private IButtonClick iButtonClick;
    private LinearLayout root_layout;

    private int pos1 = 0, pos2 = 0; // 记录省市下标

    public void setDefItem() {
        pickProvinceAdapter.setDefItem(0);
    }

    public interface IButtonClick {
        void next(String no);
    }

    public void setiButtonClick(IButtonClick iButtonClick) {
        this.iButtonClick = iButtonClick;
    }

    public PickAreaPopWindow(Context context) {
        super(context);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_pick;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recyclerView = holder.getPop().findViewById(R.id.rlv_list);
        cityList = holder.getPop().findViewById(R.id.city_list);
        bottomLayout = holder.getPop().findViewById(R.id.bottom_layout);
        ll_next = holder.getPop().findViewById(R.id.ll_next);
        tv_next = holder.getPop().findViewById(R.id.tv_next);
        tv_ok = holder.getPop().findViewById(R.id.tv_ok);
        root_layout = holder.getPop().findViewById(R.id.root_layout);

        tv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isNext) {
                    if (!TextUtils.isEmpty(oneNo)) {
                        if (onResultClick != null) {
                            if (oneNo == null)
                                oneNo = "";
                            onResultClick.result(oneNo, name, true);
                            Log.d("省份编号", oneNo);
                        }
                    }
                    if(mAreaListener != null){
                        mAreaListener.onAreaResult(pos1, pos2, pickCityAdapter.getDatas());
                    }
                } else {
                  /*  if (towNo.equals("")) {
                        if (onResultClick != null) {
                            if (towNo == null)
                                towNo = "";
                            onResultClick.result(towNo, name, position);
                            Log.d("城市编号", towNo);
                        } else {
                            if (onResultClick != null) {
                                if (oneNo == null)
                                    oneNo = "";
                                onResultClick.result(oneNo, name, position);
                                Log.d("省份编号", oneNo);
                            }
                        }
                    }*/
                    if (onResultClick != null) {
                        if (oneNo == null)
                            oneNo = "";
                        onResultClick.result(oneNo, name, true);
                        Log.d("省份编号", oneNo);
                    }
                    if(mAreaListener != null){
                        mAreaListener.onAreaResult(pos1, pos2, pickCityAdapter.getDatas());
                    }
                }
                dissMiss();
            }
        });

        tv_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isNext) {
                    if (oneNo == null)
                        oneNo = "";
                    if (!oldNo.equals(oneNo)) {
                        oldNo = oneNo;
                        iButtonClick.next(oneNo);
                    } else {
                        if (!oneNo.equals("")) {
                            setGone(View.GONE, View.VISIBLE, "上一级", true);
                        } else {
                            MToast.showToast(mContext,"请选择一个省份");
                        }

                    }
                } else {
                    setGone(View.VISIBLE, View.GONE, "下一级", false);
                }

            }
        });
        pickProvinceAdapter = new PickAreaAdapter(mContext, null, R.layout.item_pick_area_choose);
        pickProvinceAdapter.setItemSelect(true);
        pickProvinceAdapter.setDefItem(-1);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        recyclerView.setAdapter(pickProvinceAdapter);
        pickProvinceAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                oneNo = pickSelectEntity.getNo();
                name = pickSelectEntity.getProjectscale();
                pos1 = position;
                pos2 = 0;
                if (name.equals("全国"))
                    if (onResultClick != null) {
                        if (oneNo == null)
                            oneNo = "";
                        onResultClick.result(oneNo, name, true);
                        Log.d("省份编号", oneNo);
                        dissMiss();
                    }

            }
        });
        pickCityAdapter = new PickAreaAdapter(mContext, null, R.layout.item_pick_area_choose);
        pickCityAdapter.setItemSelect(true);
        pickCityAdapter.setDefItem(-1);
        cityList.setLayoutManager(new GridLayoutManager(mContext, 4));
        cityList.setAdapter(pickCityAdapter);
        pickCityAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                towNo = pickSelectEntity.getNo();
                name = pickSelectEntity.getProjectscale();
                pos2 = position;
                if (onResultClick != null) {
                    if (towNo == null)
                        towNo = "";
                    onResultClick.result(towNo, name, false);
                    Log.d("城市编号", towNo);
                    dissMiss();
                }
            }
        });
    }

    public void setData(List<PickSelectEntity> cityDatas) {
        this.cityDatas = cityDatas;
        pickProvinceAdapter.setDatas(cityDatas);

    }

    public void setData(List<PickSelectEntity> cityDatas, int position) {
        this.cityDatas = cityDatas;
        pickProvinceAdapter.setDatas(cityDatas);
        pickProvinceAdapter.setDefItem(position);
    }

    public void isVisibleBottomLayout(boolean visible) {
        ll_next.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
    }

    public void isVisibleNextButton(boolean visible) {
        tv_next.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    public void setCityDatas(List<PickSelectEntity> cityDatas) {
        if (cityDatas != null && cityDatas.size() != 0) {
            pickCityAdapter.setDefItem(-1);
            setGone(View.GONE, View.VISIBLE, "上一级", true);
            pickCityAdapter.setDatas(cityDatas);
            isNext = true;

        }
    }

    private void setGone(int isV, int isV2, String text, boolean is) {
        recyclerView.setVisibility(isV);
        cityList.setVisibility(isV2);
        tv_next.setText(text);
        isNext = is;
    }

    public void setData(List<PickSelectEntity> cityDatas, boolean isAll) {
        this.cityDatas = cityDatas;
        pickProvinceAdapter.setDatas(cityDatas);
        if (isAll)
            pickCityAdapter.setDefItem(0);

    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (onDisMiss != null)
            onDisMiss.disMiss();
    }

    public void setCityData(List<PickSelectEntity> cityDatas, int position) {
        if (cityDatas != null && cityDatas.size() != 0) {
            pickCityAdapter.setDefItem(position);
            setGone(View.GONE, View.VISIBLE, "上一级", true);
            pickCityAdapter.setDatas(cityDatas);
            isNext = true;

        }
    }

    public void setBgColor(int bgColor) {
        root_layout.setBackgroundColor(bgColor);
    }

    private AreaListener mAreaListener;

    public void setAreaListener(AreaListener areaListener) {
        mAreaListener = areaListener;
    }

    public interface AreaListener {
        void onAreaResult(int pos1, int pos2, List<PickSelectEntity> list);
    }
}
